//
//  LHActionSheet.m
//  WXYView
//
//  Created by lehuo on 2017/1/10.
//  Copyright © 2017年 lehuo. All rights reserved.
//

#import "LHActionSheet.h"
#define BackgroundColor [UIColor colorWithRed:129/255.0 green:129/255.0 blue:129/255.0 alpha:0.5]
#define kSceenW [UIScreen mainScreen].bounds.size.width
#define kSceenH [UIScreen mainScreen].bounds.size.height
#define ActionSheetBtnSpace 0.5
#define ActionSheetBtnHeight 50
//ActionSheet title字体
#define ActionSheetTitleFont  [UIFont systemFontOfSize:13]
//ActionSheet title左右间距
#define ActionSheetTitleSpace 30
@interface LHActionSheet()
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *destructiveTitle;
@property(nonatomic,strong) NSArray *otherTitles;
@property(nonatomic,strong)UIView * bgView;
@property(nonatomic,assign)NSUInteger index;//标示选项按钮的tag

@property (nonatomic,copy) PopSelectIndex  selectIndex;
@end
@implementation LHActionSheet
#pragma mark - 显示ActionSheet
+ (void)showActionSheetWithTitle:(NSString *)title destructiveTitle:(NSString *)destructiveTitle otherTitles:(NSArray <NSString *>*)otherTitles block:(PopSelectIndex)popSelectIndex
{
    
    LHActionSheet *popView = [[LHActionSheet alloc] initWithFrame:[UIScreen mainScreen].bounds];
    popView.title = title;
    popView.destructiveTitle = destructiveTitle;
    popView.otherTitles = otherTitles;
    popView.backgroundColor = BackgroundColor;
    popView.selectIndex = popSelectIndex;
    popView.index = 0;
    [popView show];
    [[UIApplication sharedApplication].keyWindow addSubview:popView];
}

#pragma mark - 添加控件
- (void)show
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor clearColor];
    self.bgView = bgView;
    [self addSubview:bgView];
    UIButton *titleBtn  = [[UIButton alloc] init];
    titleBtn.titleLabel.font = ActionSheetTitleFont;
    [titleBtn setTitle:self.title forState:UIControlStateNormal];
    titleBtn.titleLabel.numberOfLines = 0;
    titleBtn.backgroundColor = [UIColor whiteColor];
    titleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    CGSize size = [self.title boundingRectWithSize:CGSizeMake(kSceenW - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titleBtn.titleLabel.font} context:nil].size;
    titleBtn.frame = CGRectMake(0,0, kSceenW, size.height + 30);
    titleBtn.contentEdgeInsets = UIEdgeInsetsMake(ActionSheetTitleSpace, ActionSheetTitleSpace, ActionSheetTitleSpace, ActionSheetTitleSpace);
    [bgView addSubview:titleBtn];
    
    float originY = size.height + 30;
    for (int index = 0; index < self.otherTitles.count; index ++)
    {
        if(index != 0)
            originY = originY + (ActionSheetBtnHeight + ActionSheetBtnSpace);
        UIButton *btn = [self createTitleBtn:self.otherTitles[index] fontColor:[UIColor lightGrayColor] originY:originY];
        btn.tag = index;
        [bgView addSubview:btn];
        self.index = index;
    }
    if (self.otherTitles.count >0)
        originY = originY + ActionSheetBtnHeight + ActionSheetBtnSpace;
    if (self.destructiveTitle){
    UIButton * destructiveBtn = [self createTitleBtn:self.destructiveTitle fontColor:[UIColor redColor] originY:originY];
        self.index ++;
        destructiveBtn.tag = self.index;
    [bgView addSubview:destructiveBtn];
    destructiveBtn.tag = self.otherTitles.count;
        originY = originY + ActionSheetBtnHeight;
    }
    
    
    UIButton * cancelBtn = [self createTitleBtn:@"取消" fontColor:[UIColor blackColor]  originY:originY + 5];
//    cancelBtn.tag = destructiveBtn.tag + 1;
    cancelBtn.frame = CGRectMake(0, originY + 5, kSceenW, ActionSheetBtnHeight);
    cancelBtn.tag = ++self.index;
    [bgView addSubview:cancelBtn];
    originY = originY + 5 + ActionSheetBtnHeight;
    bgView.frame = CGRectMake(0, kSceenH, kSceenW, originY);
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = bgView.frame;
        frame.origin.y = kSceenH - originY;
        bgView.frame = frame;
    }];
    
}

#pragma mark - 添加选项按钮
- (UIButton *)createTitleBtn:(NSString *)title fontColor:(UIColor *)color  originY:(float)y
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, y + ActionSheetBtnSpace, kSceenW, ActionSheetBtnHeight);
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

#pragma mark - 单击事件
- (void)titleBtnClick:(UIButton *)btn
{
    self.selectIndex(btn.tag);
    [UIView animateWithDuration:0.3 animations:^{
        CGRect newFrame = self.bgView.frame;
        newFrame.origin.y = self.frame.origin.y + kSceenH;
        self.bgView.frame = newFrame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

#pragma mark - 根据背景色来生成图片
-(UIImage*)imageWithColor:(UIColor*)color
{
    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddRect(context, CGRectMake(0, 0, 1, 1));
    [color set];
    CGContextFillPath(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

